package stack_queue;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: 1047. 删除字符串中的所有相邻重复项
 * User: Li_yizYa
 * Date: 2025/5/18
 * Time: 17:41
 */
public class Solution4 {
    private static String removeDuplicates(String s) {
        if (s == null || s.length() == 0) {
            return null;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!stack.isEmpty()) {
                char tmp = stack.peek();
                if (c == tmp) {
                    stack.pop();
                    continue;
                }
            }
            stack.push(c);
        }
        StringBuilder ret = new StringBuilder();
        for (char c : stack) {
            ret.append(c);
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        System.out.println(removeDuplicates("abbaca"));
        System.out.println(removeDuplicates("aaaaaaaaa"));
        System.out.println(removeDuplicates("abcdefg"));
    }
}
